package me.ohaga
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	import net.flashpunk.Tween;
	import net.flashpunk.World;
	import net.flashpunk.graphics.Emitter;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.tweens.misc.NumTween;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Ease;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	public class G extends World
	{
		private static const Y:Number = 100;
		private static const ENDX:Number = 500;
		public static const COLOR:uint = 0xFF0000; 
		
		private var _state:String = "title";
		private const MEN_SEL_YPOS:uint = 303;
		private const MEN_SEL_XPOS:uint = 570;
		private const OPT_SEL_XPOS:uint = 470;
		
		private var x:Number = 0;
		private var _menuCount:int = 0;
		private var _partyEntity:Entity;
		
		private var _tutTween:NumTween = new NumTween();
		private var _quitTween:NumTween = new NumTween();
		private var _soundTween:NumTween = new NumTween();

		
		[Embed(source="/assets/gfx/particle.png")] private static const P:Class;
		
		[Embed(source="/assets/sfx/splode.mp3")] private static const SPLODE:Class;
		[Embed(source="/assets/sfx/error.mp3")] private static const ERROR:Class;
		[Embed(source="/assets/sfx/tuneloop.mp3")] private static const TUNE:Class;

		public var sfxSplode:Sfx = new Sfx(SPLODE);
		public var sfxError:Sfx = new Sfx(ERROR);
		public var sfxTune:Sfx = new Sfx(TUNE);
		
		private var _party:Emitter = new Emitter(P,1,1);
		
		public function G() { 
			super();
			_party.newType("p", [0]);
			_party.setMotion("p",0,40,1,360,30,2);
			_party.setGravity("p",-1,6);
		}
		
		override public function begin():void
		{
			super.begin();
			sfxTune.loop(.7);
			showTitle();
			//playGame();
			
		}
		
		private function showTitle():void
		{
			FP.world.removeAll();
			_state = "title";
			_menuCount = 0;
			x=0;
			
			addGraphic(new Text("min", FP.width - 200, 60, { size: 72, color: 0xFFFFFF } ));
			addGraphic(new Text("Play", FP.width - 200, 300, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("Options", FP.width - 200, 350, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("Quit", FP.width - 200, 400, { size: 24, color: 0xFFFFFF } ));
			

		}
		
		private function showOptions():void
		{
			FP.world.removeAll();
			_menuCount = 0;
			_state = "options";
			
			addGraphic(new Text("Options", FP.width - 300, 60, { size: 72, color: 0xFFFFFF } ));
			addGraphic(new Text("Graphics", FP.width - 300, 300, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("Difficulty", FP.width - 300, 350, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("min", FP.width - 175, 300, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("min", FP.width - 175, 350, { size: 24, color: 0xFFFFFF } ));
			addGraphic(new Text("max", FP.width - 100, 300, { size: 24, color: 0x555555 } ));
			addGraphic(new Text("max", FP.width - 100, 350, { size: 24, color: 0x555555 } ));
			addGraphic(new Text("Back", FP.width - 300, 400, { size: 24, color: 0xFFFFFF } ));
		}
		
		private function quitting():void
		{
			//FP.log("let's quit");
			_state = "quit";
			_quitTween.tween(0,FP.halfHeight,1.3);
			_soundTween.tween(sfxTune.volume,0, 1.3);
			addTween(_soundTween, true);
			addTween(_quitTween,true);
		}
		
		private function playGame():void
		{
			x = 0;
			FP.world.removeAll();
			_state = "game";
			add(new B(0));
			_tutTween.tween(10,0,10);
			addTween(_tutTween, true);
		}
		
		override public function update():void
		{
			super.update();
			var b:B;
			
			if(_state == "title")
			{
				if(Input.pressed(Key.DOWN) || Input.pressed(Key.UP))
				{
					b = new B(MEN_SEL_XPOS,(MEN_SEL_YPOS + 50*_menuCount), 1);
					add(b);
					if(Input.pressed(Key.UP)) {
						_menuCount = Math.max(_menuCount-1,0)
					} else {
						_menuCount = Math.min(_menuCount+1,2)
					}
					b.start();
				}
				if(Input.pressed(Key.ENTER))
				{
					if (_menuCount == 0)
						playGame();
					else if (_menuCount == 1)
						showOptions();
					else if (_menuCount == 2)
						quitting();
				}
				
			}
			
			if(_state == "game")
			{
				if (x < ENDX && Input.check(Key.RIGHT)) {
					if (_tutTween.active) {
						_tutTween.cancel();
					}
					
					x = Math.min(x + 2, ENDX);
					if (x % B.W == 0 && x > 0) {
						add(new B(x));
						// Start fading the block to the left once the main block is completely past it
						b = collidePoint("b", x-B.W/2, Y + B.H/2) as B;
						if (null != b) {
							b.start();
						}
					}
				}
			}
			
			if(_state == "credits")
			{
				if(Input.pressed(Key.ENTER))
				{
					showTitle();
				}
			}
			
			if(_state == "options")
			{
				if(Input.pressed(Key.DOWN) || Input.pressed(Key.UP))
				{
					b = new B(OPT_SEL_XPOS,(MEN_SEL_YPOS + 50*_menuCount), 1);
					add(b);
					if(Input.pressed(Key.UP)) {
						_menuCount = Math.max(_menuCount-1,0)
					} else {
						_menuCount = Math.min(_menuCount+1,2)
					}
					b.start();
				}
				if(Input.pressed(Key.ENTER))
				{
					if (_menuCount == 2)
					    showTitle();
				}
				if((Input.pressed(Key.LEFT) || Input.pressed(Key.RIGHT)) && _menuCount != 2)
				{
					sfxError.play();
				}	
						
			}
			
			if(_state == "quit")
			{
				sfxTune.volume = _soundTween.value;
				if (!_quitTween.active) {
					if (Input.pressed(Key.ENTER)) {
						//showTitle();
						FP.world = new G;
					}
				}
			}
			
			if (null != _partyEntity && 0 == _party.particleCount) {
				remove(_partyEntity);
				_partyEntity = null;
			}
		}
		
		override public function render():void {
			super.render();
			if(_state != "credits" || x < ENDX)
				Draw.rect(x,Y,B.W,B.H,COLOR,1);
			
			if (_state == "title") {
				Draw.rect(MEN_SEL_XPOS,(MEN_SEL_YPOS + 50*_menuCount),B.W,B.H,COLOR,1);
			} else if (_state == "options") {
				Draw.rect(OPT_SEL_XPOS,(MEN_SEL_YPOS + 50*_menuCount),B.W,B.H,COLOR,1);
			}
			
			if (_state == "game") {
				if (_tutTween.active && _tutTween.value <= 5) {
					Draw.linePlus(B.W *1.5,Y+B.H/2, B.W*2.5, Y+B.H/2, COLOR, _tutTween.value/5, 2);
					Draw.linePlus(B.W *2,Y, B.W*2.5, Y+B.H/2, COLOR, _tutTween.value/5, 2);
					Draw.linePlus(B.W *2,Y+B.H, B.W*2.5, Y+B.H/2, COLOR, _tutTween.value/5, 2);
				}
			}
			
			if (_state == "quit") {
				Draw.rect(0,0,FP.width,_quitTween.value, 0x000000);
				var y:Number = FP.height - _quitTween.value
				Draw.rect(0, y, FP.width, FP.height - y, 0x000000);
			}
		}
		
		public function amISeeingThings():void {
			if (null == _partyEntity) {
				_partyEntity = addGraphic(_party, 0, x + B.W/2, Y+B.H/2);
				for (var i:Number = 0; i < 300; i++) {
					_party.emit("p",0,0);
				}
				sfxSplode.play()
			}
		}
		
		public function boom(b:B):void {
			if(b.y != Y)
			{
				remove(b);
				return;
			}
			if (x - b.x < B.W*2 && x < ENDX && x > 20) {
				rollCredits(true);
			} else if (x - b.x < B.W && x >= ENDX) {
				amISeeingThings();
				rollCredits(false);
			}
			remove(b);
		}
		
		
		public function rollCredits(lose:Boolean):void
		{
			FP.world.removeAll();
			_state = "credits";
			
			if(lose)
			{				
				addGraphic(new Text("fin", FP.width - 200, 60, { size: 72, color: 0xFFFFFF } ));
				addGraphic(new Text("(not win)", FP.width - 188, 120, { size: 16, color: 0xFFFFFF } ));
			}
			else
				addGraphic(new Text("win", FP.width - 200, 60, { size: 72, color: 0xFF0000 } ));

			
			addGraphic(new Text("By:  TF & JP", FP.width - 220, 450, { size: 24, color: 0xFFFFFF } ));
		}

	}
}